package com.igor.bookStore.tapestry.components;

import java.util.Locale;
import org.apache.tapestry5.Asset;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PersistentLocale;

public class SwitchLocale {
	@Inject
	@Path("context:/assets/flag.gif")
	private Asset flag;
	    
	public Asset getFlag() {
	  return flag;
	}

	@Inject 
	private PersistentLocale persistentLocale;
	
	@Inject
	private Locale currentLocale;

	@Persist
	private String localeLabel;
	    
	@OnEvent(component="switchlocale")
	void changeLocale() {
		if (currentLocale.equals(new Locale("sr"))) {
			persistentLocale.set(Locale.ENGLISH);
		}
		else {
			persistentLocale.set(new Locale("sr", "ME"));
		}
	}

	public String getLocaleLabel() {
		if (currentLocale.equals(new Locale("sr", "ME"))) {
			localeLabel = new Locale("en").getDisplayName(Locale.ENGLISH);
		}
		else {
			localeLabel = new Locale("sr", "ME").getDisplayName();
		}
		return localeLabel; 
	}
}
